/**
 * 
 */
package cc.rico.jms.topic.subcriber;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cc.rico.jms.topic.listener.TopicListener;
import cc.rico.jms.utils.ActiveMQConnectionUtils;

/**
 * @author rico 2016年8月2日
 *
 */
public class SimpleSubscriber {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(SimpleSubscriber.class);
	
	private TopicConnection connection;
	private TopicSession session;
	
	public SimpleSubscriber() {
		try {
			//TopicConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.223.137:61616");
			//connection = connectionFactory.createTopicConnection();
			connection = ActiveMQConnectionUtils.createTopicConnection();
			connection.setClientID("topicClient");
			
			session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic("topic1");
			
			//TopicSubscriber subscriber = session.createDurableSubscriber(topic, "sub1");
			TopicSubscriber subscriber = session.createSubscriber(topic);
			subscriber.setMessageListener(new TopicListener());
			
			connection.start();
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
